#include "stdafx.h"
#include "wf_libraryinsert.h"

#include "pf_creation.h"
#include "pf_commands.h"
#include "pf_modification.h"



/**
 * Constructor.
 */
WF_LibraryInsert::WF_LibraryInsert(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Library Insert",
                           container, graphicView) {}



WF_LibraryInsert::~WF_LibraryInsert() {}


QAction* WF_LibraryInsert::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
/*      QAction* action = new QAction(tr("Insert Library Object"),
                                  tr("&Insert Library Object"),
                                  QKeySequence(), NULL); */
    QAction* action = new QAction(tr("Insert Library Object"), NULL);
    //action->zetStatusTip(tr("Inserts an Object from the part library."));
    return action;
}


void WF_LibraryInsert::init(int status) {
    PF_PreviewActionInterface::init(status);

    reset();

    /*if (graphic!=NULL) {
        block = graphic->getActiveBlock();
        if (block!=NULL) {
            data.name = block->getName();
        }
}*/
    //trigger();
}



void WF_LibraryInsert::setFile(const PF_String& file) {
    data.file = file;

    if (!prev.open(file, LX_Define::FormatUnknown)) {
        PF_DIALOGFACTORY->commandMessage(tr("Cannot open file '%1'").arg(file));
    }
}


void WF_LibraryInsert::reset() {
    /*data = PF_InsertAttribute("",
                         PF_Vector(0.0,0.0),
                         PF_Vector(1.0,1.0),
                         0.0,
                         1, 1,
                         PF_Vector(1.0,1.0),
                         NULL,
                         LX_Define::Update);*/

    data.insertionPoint = PF_Vector(false);
    data.factor = 1.0;
    data.angle = 0.0;
}



void WF_LibraryInsert::trigger() {
    deletePreview();

    PF_Creation creation(container, graphicView);
    creation.createLibraryInsert(data);

	graphicView->redraw(LX_Define::RedrawDrawing); 

}


void WF_LibraryInsert::mouseMoveEvent(PF_MouseEvent* e) {
    switch (getStatus()) {
    case SetTargetPoint:
        data.insertionPoint = snapPoint(e);

        //if (block!=NULL) {
        deletePreview();
        preview->addAllFrom(prev);
        preview->move(data.insertionPoint);
        preview->scale(data.insertionPoint,
                       PF_Vector(data.factor, data.factor));
        // unit conversion:
        if (graphic!=NULL) {
            double uf = PF_Units::convert(1.0, prev.getUnit(),
                                          graphic->getUnit());
            preview->scale(data.insertionPoint,
                           PF_Vector(uf, uf));
        }
        preview->rotate(data.insertionPoint, data.angle);
        // too slow:
        //PF_Creation creation(preview, NULL, false);
        //creation.createInsert(data);
        drawPreview();
        //}
        break;

    default:
        break;
    }
}



void WF_LibraryInsert::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        init(getStatus()-1);
    }
}



void WF_LibraryInsert::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    data.insertionPoint = e->getCoordinate();
    trigger();
}



void WF_LibraryInsert::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }

    switch (getStatus()) {
    case SetTargetPoint:
        if (checkCommand("angle", c)) {
            deletePreview();
            lastStatus = (Status)getStatus();
            setStatus(SetAngle);
        } else if (checkCommand("factor", c)) {
            deletePreview();
            lastStatus = (Status)getStatus();
            setStatus(SetFactor);
        }
        break;

    case SetAngle: {
            bool ok;
            double a = PF_Math::eval(c, &ok);
            if (ok==true) {
                data.angle = PF_Math::deg2rad(a);
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(lastStatus);
        }
        break;

    case SetFactor: {
            bool ok;
            double f = PF_Math::eval(c, &ok);
            if (ok==true) {
                setFactor(f);
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(lastStatus);
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_LibraryInsert::getAvailableCommands() {
    PF_StringList cmd;

    switch (getStatus()) {
    case SetTargetPoint:
        cmd += command("angle");
        cmd += command("factor");
        ;
        break;
    default:
        break;
    }

    return cmd;
}


void WF_LibraryInsert::showOptions() {
    PF_ActionInterface::showOptions();

    PF_DIALOGFACTORY->requestOptions(this, true);
}



void WF_LibraryInsert::hideOptions() {
    PF_ActionInterface::hideOptions();

    PF_DIALOGFACTORY->requestOptions(this, false);
}


void WF_LibraryInsert::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetTargetPoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
                                            tr("Cancel"));
        break;
    case SetAngle:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"),
                                            "");
        break;
    case SetFactor:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"),
                                            "");
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_LibraryInsert::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_LibraryInsert::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarMain);
    }
}


// EOF
